// 拼接HTTPUrl
function getUrl(type, pathname) {
  return window.RPConfig[type] + pathname;
}

const coverApi = {
  // 获取当前登录人有权访问的表类型实施方案列表
  getPermittedTableList: () =>{
    return $http.post(getUrl('itShare', '/rptApproverTree/getTableId'))
  },
  //实施方案列表
  getCoverTableList:params =>{
    let url = getUrl('itShare', '/wfRptPlantask/page')
    if (params.type == 'Summary') {
      delete params.data.allDataFlag
      url = getUrl('itShare', '/rptPlantask/page')
    }
    return $http.post(url, params.data);
  },

  //集团实施方案列表
  getGroupTableList:params =>{
    let url = getUrl('itShare', '/wfGroupRptPlantask/page')
    return $http.post(url, params.data);
  },


  // 兼容publicpage模式
  getCoverDetail: params => {
    let token = params.token
    let url = getUrl('itShare', '/wfRptPlantask/getById')
    if (params.type == 'Summary') {
      url = getUrl('itShare', '/rptPlantask/getById')
    }
    if (token) {
      delete params.token
      return $http.get(url, params.data, null, {
        headers: { authorization: token }
      });
    } else {
      return $http.get(url, params.data);
    }
  },

  //新建
  coverAdd: params => {
    return $http.post(getUrl('itShare', `/wfRptPlantask/insert`), params);
  },
  coverDelete:params => {
    let url = getUrl('itShare', '/wfRptDgcalHrhxStatus/delete')
    if (params.type == 'Summary') {
      url = getUrl('itShare', '/rptDgcalHrhxStatus/delete')
    }
    return $http.post(url, params.data);
  },
  coverDeleteplant:params => {
    let url = getUrl('itShare', '/wfRptPlantask/delete')
    if (params.type == 'Summary') {
      url = getUrl('itShare', '/rptPlantask/delete')
    }
    return $http.post(url, params.data);
  },

  // 删除接口
  coverDeleteData:params => {
    let url = getUrl('itShare', '/wfRptPlantask/remove')
    if (params.type == 'Summary') {
      url = getUrl('itShare', '/rptPlantask/remove')
    }
    return $http.post(url, params.data);
  },
  //中期修改编辑接口
  coverAdminUpdate: params => {
    let url = getUrl('itShare', '/wfRptPlantask/admin/update')
    return $http.post(url, params.data);
  },
  // 中期删除接口
  coverDeleteMidtermData:params => {
    let url = getUrl('itShare', '/wfRptPlantask/admin/remove')
    return $http.post(url, params.data);
  },

  //编辑
  coverUpdate: params => {
    let url = getUrl('itShare', '/wfRptPlantask/update')
    if (params.type == 'Summary') {
      url = getUrl('itShare', '/rptPlantask/update')
    }
    return $http.post(url, params.data);
  },
  exportwfRptPlantask: params => {
    return $http.get(getUrl('itShare', `/wfRptPlantask/listExport`), params);
  },
  // 数字化覆盖目标、覆盖状态
  getCoverTargetList: params => {
    let url = getUrl('itShare', '/wfRptDgcalHrhxStatus/page')
    if (params.type == 'Summary') {
      url = getUrl('itShare', '/rptDgcalHrhxStatus/page')
    }
    return $http.post(url, params.data);
  },
  // 新增覆盖率
  addCoverRate: params => {
  return $http.post(getUrl('itShare', `/wfRptDgcalHrhxStatus/insert`), params);
  },
  // 编辑覆盖率
  updateCoverRate: params => {
    let url = getUrl('itShare', '/wfRptDgcalHrhxStatus/update')
    if (params.type == 'Summary') {
      url = getUrl('itShare', '/rptDgcalHrhxStatus/update')
    }
    return $http.post(url, params.data);
  },
  // 获取覆盖现状详情
  getCoverStatusById: params => {
    let url = getUrl('itShare', '/wfRptDgcalHrhxStatus/getById')
    if (params.type == 'Summary') {
      url = getUrl('itShare', '/rptDgcalHrhxStatus/getById')
    }
    return $http.get(url,params.data);
  },
  exportwfRptDgcalHrhxStatus: params => {
    return $http.get(getUrl('itShare', `/wfRptDgcalHrhxStatus/listExport`), params);
  },

  //获取值集管理
  getCodeManage:params =>{
    return $http.post(getUrl('setting', "/valueset/item/list/page"),params)
  },
  // 新增数字化覆盖率计算表
  addwfRptDgCal: params => {
    return $http.post(getUrl('itShare', '/wfRptDgCal/insert'),params);
  },
  // 数字化覆盖率计算表分页
  getwfRptDgCalList: params => {
    let url = getUrl('itShare', '/wfRptDgCal/page')
    if (params.type == 'Summary') {
      url = getUrl('itShare', '/rptDgCal/page')
    }
    return $http.post(url,params.data);
  },
  // 数字化覆盖率计算表删除
  deletewfRptDgCal: params => {
    let url = getUrl('itShare', '/wfRptDgCal/delete')
    if (params.type == 'Summary') {
      url = getUrl('itShare', '/rptDgCal/delete')
    }
    return $http.post(url,params.data);
  },
  // 数字化覆盖率计算表获取详情
  getwfRptDgCalById: params => {
    let url = getUrl('itShare', '/wfRptDgCal/getById')
    if (params.type == 'Summary') {
      url = getUrl('itShare', '/rptDgCal/getById')
    }
    return $http.get(url,params.data);
  },
  // 数字化覆盖率计算表编辑
  updatewfRptDgCal: params => {
    let url = getUrl('itShare', '/wfRptDgCal/update')
    if (params.type == 'Summary') {
      url = getUrl('itShare', '/rptDgCal/update')
    }
    return $http.post(url,params.data);
  },
   // 化覆盖率计算表导出
   exportwfRptDgCal: params => {
    return $http.get(getUrl('itShare', `/wfRptDgCal/listExport`), params);
  },
  // 数字化覆盖目标分页
  getwfRptDgcalHrhxList: params => {
    let url = getUrl('itShare', '/wfRptDgcalHrhx/page')
    if (params.type == 'Summary') {
      url = getUrl('itShare', '/rptDgcalHrhx/page')
    }
    return $http.post(url,params.data);
  },
  // 数字化覆盖目标删除
  deletewfRptDgcalHrhx: params => {
    let url = getUrl('itShare', '/wfRptDgcalHrhx/delete')
    if (params.type == 'Summary') {
      url = getUrl('itShare', '/rptDgcalHrhx/delete')
    }
    return $http.post(url,params.data);
  },
  // 数字化覆盖目标新增
  addwfRptDgcalHrhx: params => {
    return $http.post(getUrl('itShare', '/wfRptDgcalHrhx/insert'),params);
  },
  // 数字化覆盖目标获取详情
  getwfRptDgcalHrhxById: params => {
    let url = getUrl('itShare', '/wfRptDgcalHrhx/getById')
    if (params.type == 'Summary') {
      url = getUrl('itShare', '/rptDgcalHrhx/getById')
    }
    return $http.get(url,params.data);
  },
  // 数字化覆盖目标编辑
  updatewfRptDgcalHrhx: params => {
    let url = getUrl('itShare', '/wfRptDgcalHrhx/update')
    if (params.type == 'Summary') {
      url = getUrl('itShare', '/rptDgcalHrhx/update')
    }
    return $http.post(url,params.data);
  },
  // 数字化覆盖目标导出
  exportwfRptDgcalHrhx: params => {
    return $http.get(getUrl('itShare', `/wfRptDgcalHrhx/listExport`), params);
  },
  getRevisionRecord: params => {
    return $http.post(getUrl('itShare', '/logRecord/findList'),params);
  },
  // 报表批量操作--提交
  commitwfRptDgcalHrhx: params => {
    return $http.post(getUrl('itShare', '/wfRptPlantask/batch/commit'),params, null, {
      timeout: 40 * 1000
    });
  },
  // 现状表提交
  statusCommit: params => {
    return $http.post(getUrl('itShare', '/wfRptDgcalHrhxStatus/batch/commit'),params.data);
  },
  // 目标表提交
  targetCommit: params => {
    return $http.post(getUrl('itShare', '/wfRptDgcalHrhx/batch/commit'),params.data);
  },
  // 计算表提交
  rateCommit: params => {
    return $http.post(getUrl('itShare', '/wfRptDgCal/batch/commit'),params.data);
  },
  //获取值集管理
  getCodeList:code =>{
    let info =  {
      commonPageDTO:{currentPage: 1,pageSize:1000},
      condition:{tenantCode:"IT_SHARE_DEVELOP",vsCode:code},
      pageNum:1,
      pageSize: 1000,
      sortDTO:{fieldName: "",orderBy: ""},
      tenantCode: "IT_SHARE_DEVELOP",
      vsCode: code
    }
    return $http.post(getUrl('setting', "/valueset/item/list/page"),info)
  },
  // 获取值集管理中的值名称
  getLangVos: vsiId => {
    return $http.get(getUrl('setting', `/valueset/item/lang/list?vsiId=${vsiId}`)).then(res => res.data)
  },
  // 修改值集管理
  updateCodeList: data =>{
    let info =  {
      "version": 1,
      "id": data.id,
      "sort": 1,
      "vsCode": data.vsCode,
      "vsiKey": data.vsiKey,
      "enabled": "1",
      valueSetItemLangVos: data.valueSetItemLangVos,
      // "vsiStartDate": data.,
      // "vsiEndDate": "2099-12-31",
      "tenantCode": "itshare-sit",
      "vsiParentKey": "",
      "vsiDescription": data.vsiDescription,
  }
    return $http.post(getUrl('setting', "/valueset/item/update"),info)
  },
  downLoadFun: params => {
    const link = document.createElement('a')
    link.style.display = 'none'
    link.href = params.url //创建下载的链接
    link.setAttribute('download', 'fillname')//下载文件名
    document.body.appendChild(link)
    link.click()//点击下载
    link.remove()//下载完成移除元素
  },
  dataPass: params => {
    return $http.get(getUrl('itShare', "/todoCenter/data/pass"),params)
  },
  dataReject: params => {
    return $http.get(getUrl('itShare', "/todoCenter/data/reject"),params)
  },
  getRptTrgnodeTree: params => {
    return $http.post(getUrl('itShare', "/rptTrgnodeTree/findListByUser"),params)
  },
  getReceiverGroup: params => {
    return $http.get(getUrl('itShare', "/itsReceiverGroup/tree"),params)
  },
  getRptReportTrgnode: params => {
    return $http.post(getUrl('itShare', "/rptReportTrgnode/page"),params)
  },
  getTreeList: params => {
    return $http.post(getUrl('itShare', "/rptTrgNodeAppConfig/findTreeList"),params)
  },
  getColumnInfo(params) {
    return $http.get(getUrl('itShare','/rptUserColumnSetting/getOneByMenuNo'), params).then(res => res.data)
  },
  insertColumn(params) {
    return $http.post(getUrl('itShare', '/rptUserColumnSetting/insert'), params)
  },
  updateColumn(params) {
    return $http.post(getUrl('itShare', '/rptUserColumnSetting/update'), params)
  },
  downloadFile: (tableName, trgNode) => {
    const urlMap = {
      '总任务表': '',
      '工期图': '/gantDateCommon/listExport',
      '数字化覆盖率计算表': '/rptDgCal/listExport',
      '2022年数字化覆盖现状': '/rptDgcalHrhxStatus/listExport',
      '“十四五”数字化覆盖目标': '/rptDgcalHrhx/listExport',
    }
    let url = urlMap[tableName]
    if (!url) {
      console.error(`根据表名【${tableName}】未获取到导出汇总的url`)
      return
    }
    return $http.get(getUrl('itShare', url+`?trg_node=${trgNode}`))
  },
  downloadFile2: (url,data = null, fileName = '', renderData = true) => {
    const downloadUrl = url
    const xhr = new XMLHttpRequest()
    xhr.open('POST', downloadUrl, true)
    xhr.setRequestHeader('authorization', localStorage.Authorization ? `Bearer ${localStorage.Authorization}` : '')
    xhr.setRequestHeader('roleid', sessionStorage.getItem("RoleId") ? sessionStorage.getItem("RoleId") : '')
    xhr.setRequestHeader('Content-Type', 'application/json')
    xhr.responseType = 'blob'
    xhr.onload = (e) => {
      const _this = e.target
      if (_this.status === 200) {
        const blob = _this.response
        const a = document.createElement('a')
        a.href = URL.createObjectURL(blob)
        a.download = ''
        if (fileName) {
          a.setAttribute('download', fileName)//下载文件名
        } else {
          let headersName = decodeURI(xhr.getResponseHeader("content-disposition").split("=")[1])
          headersName = decodeURIComponent(escape(headersName))
          a.setAttribute('download', headersName)
        }
        document.body.appendChild(a)
        a.click()
        document.body.removeChild(a)
      }
    }
    let sendData = renderData ? JSON.stringify(data) : data
    xhr.send(sendData)
  },
  // 兼容publicpage模式
  getRptReportTrgnodePList: (params) => {
    let token = params.token
    if (token) {
      delete params.token
      return $http.post(getUrl('itShare', "/rptReportTrgnode/pList"), params, null, {
        headers: { authorization: token }
      })
    } else {
      return $http.post(getUrl('itShare', "/rptReportTrgnode/pList"), params)
    }
  },
  // 工作台-审批中心-待审批、已审批页面搜索条件【业务单元】下拉数据查询
  getMyApprovalBussinessUnit: () => {
    return $http.post(getUrl('itShare', "/rptApproverTree/getTrgNode "))
  },
  postRptPlantaskFindList: (params) => {
    return $http.post(getUrl('itShare', "/rptPlantask/findList"), params)
  },
  requestDownLoadGantt: (params) => {
    return $http.post(
      getUrl('itShare', '/rptPlantask/ganttChart/export'),
      params
    )
  },
  checkDownLoadGantt: (params) => {
    return $http.post(
      getUrl('itShare', '/rptPlantask/ganttChart/query'),
      params
    )
  },
  downLoadGantt: (params) => {
    return $http.post(
      getUrl('itShare', '/rptPlantask/ganttChart/download'),
      params,
      null,
      { responseType: "blob", timeout: 10 * 60 * 1000 }
    )
  },
  exportPDF: (params) => {
    return $http.post(
      getUrl('itShare', '/plantask/ganttChart/export'),
      params,
      null,
      { responseType: "blob", timeout: 10 * 60 * 1000 }
    )
  },

  // 新接口2023-0605
  // 数字化覆盖计算表
  getwfRptDgCalList2: params => {
    let url = getUrl('itShare', '/rptDgcalOperate/page')
    return $http.post(url, params.data);
  },
  // 现状表详情 operateId  trgNode
  getwfRptDgcalHrhxStatusDetail: (params) => {
    return $http.post(getUrl('itShare', `/wfRptDgcalHrhxStatus/wf/detail?operateId=${params.operateId}&trgNode=${params.trgNode}&bindId=${params.bindId}`),params)
  },
  // 目标表详情 operateId  trgNode  reportCycle
  getwfRptDgcalHrhxDetail: (params) => {
    return $http.post(getUrl('itShare', `/wfRptDgcalHrhx/wf/detail?operateId=${params.operateId}&trgNode=${params.trgNode}&reportCycle=${params.reportCycle}&bindId=${params.bindId}`),params)
  },
   // 计算表详情 operateId  trgNode
  getwfRptDgCalDetail: (params) => {
    return $http.post(getUrl('itShare', `/wfRptDgCal/wf/detail?operateId=${params.operateId}&trgNode=${params.trgNode}&bindId=${params.bindId}&rptType=${params.rptType}`),params)
  },
  //现状表查看报表
  getrptDgcalHrhxStatusListRpt: (params) => {
    return $http.post(getUrl('itShare', `/rptDgcalHrhxStatus/ListRpt?trgNode=${params.trgNode}`),params)
  },

  //目标表查看报表
  getrptDgcalHrhxListRpt: (params) => {
    return $http.post(getUrl('itShare', `/rptDgcalHrhx/ListRpt?trgNode=${params.trgNode}&reportCycle=${params.reportCycle}`),params)
  },
  //计算表查看报表
  getrptDgCalListRpt: (params) => {
    return $http.post(getUrl('itShare', `/rptDgCal/ListRpt?trgNode=${params.trgNode}`),params)
  },
  // 根据流程id获取流程详情URL /wfRptPlantask/getUrlNoHost
  getProcessUrl: (params) => {
    return $http.get(getUrl('itShare', "/wfRptPlantask/getUrlNoHost"),params)
  },
  updateTargetTable: (params) => {
    return $http.post(getUrl('itShare', `/wfRptDgcalHrhx/dynamic/update`),params)
  },
  // 查询报表信息
  gatTableInfo: (params) => {
    return $http.post(getUrl('itShare', `/rptDgcalOperate/findList`),params)
  },
  coverDelete2: params => {
    let url = getUrl('itShare', '/rptDgcalOperate/delete')
    return $http.post(url, params.data);
  },
  coverCommit2: params => {
    let url = getUrl('itShare', '/rptDgcalOperate/batch/commit')
    return $http.post(url, params.data);
  },
  downloadFile3: (reportBatch, fileName = undefined) => {
    let url = getUrl('itShare', '/rptDgcalOperate/listExport/common')+`?operateId=${reportBatch}`
    const downloadUrl = url
    const xhr = new XMLHttpRequest()
    xhr.open('GET', downloadUrl, true)
    xhr.setRequestHeader('authorization', localStorage.Authorization ? `Bearer ${localStorage.Authorization}` : '')
    xhr.responseType = 'blob'
    xhr.onload = (e) => {
      const _this = e.target
      if (_this.status === 200) {
        const blob = _this.response
        const a = document.createElement('a')
        a.href = URL.createObjectURL(blob)
        a.download = ''
        if (fileName) {
          a.setAttribute('download', fileName)//下载文件名
        } else {
          let headersName = decodeURI(xhr.getResponseHeader("content-disposition").split("=")[1])
          headersName = decodeURIComponent(escape(headersName))
          a.setAttribute('download', headersName)
        }
        document.body.appendChild(a)
        a.click()
        document.body.removeChild(a)
      }
    }
    xhr.send({})
  },
  importFile: params => {
    return new Promise((resolve, reject) => {
      const formData = new FormData()
      formData.append('authorization', localStorage.Authorization ? `Bearer ${localStorage.Authorization}` : '')
      formData.append("file", params.file)
      for (const key in params.params) {
        if (Object.hasOwnProperty.call(params.params, key)) {
          const element = params.params[key]
          formData.append(key, element)
        }
      }
      const xhr = new XMLHttpRequest()
      xhr.open('POST', params.url, true)
      xhr.setRequestHeader('Authorization', localStorage.Authorization ? `Bearer ${localStorage.Authorization}` : '')
      xhr.onloadend = () => {
        if(xhr.status === 200) {
          const json = JSON.parse(xhr.response)
          resolve(json)
        } else {
          console.log(xhr)
          if (xhr.response !== undefined && xhr.response !== null) {
            const json = JSON.parse(xhr.response)
            reject(json.message)
          } else {
            reject(xhr.statusText)
          }
        }
      }
      xhr.onerror = () => {
        if (xhr.status !== 200) {
          reject(xhr.statusText || '请检查导入模板数据后重新上传')
        }
      }
      xhr.send(formData)
    })
  },

  plantaskUpload: (params ,failCallback) => {
    let url = getUrl('itShare', '/rptFileRecord/plantask/upload')
    let config = {
      headers: {
        "Content-Type": "multipart/form-data"
      }
    };
    return $http.post(url, params,failCallback,config);
  },

  getPlantaskUploadUrl:() =>{
    return getUrl('itShare', '/rptFileRecord/plantask/upload')
  },

  plantaskDownloadFile: fileId => {
    let fileName = ''
    const data = new FormData()
    data.append('fileId', fileId)
    const downloadUrl = getUrl('itShare', '/rptFileRecord/plantask/download')
    const xhr = new XMLHttpRequest()
    xhr.open('POST', downloadUrl, true)
    xhr.setRequestHeader('authorization', localStorage.Authorization ? `Bearer ${localStorage.Authorization}` : '')
    xhr.responseType = 'blob'
    xhr.onload = (e) => {
      const _this = e.target
      if (_this.status === 200) {
        const blob = _this.response
        const a = document.createElement('a')
        a.href = URL.createObjectURL(blob)
        a.download = ''
        if (fileName) {
          a.setAttribute('download', fileName)//下载文件名
        } else {
          let headersName = decodeURI(xhr.getResponseHeader("content-disposition").split("=")[1])
          headersName = decodeURIComponent(escape(headersName))
          a.setAttribute('download', headersName)
        }
        document.body.appendChild(a)
        a.click()
        document.body.removeChild(a)
      }
    }
    // let sendData = renderData ? JSON.stringify(data) : data
    xhr.send(data)
  },

  rptDgcalHrhxFindList(params) {
    let url = getUrl('itShare', '/rptDgcalHrhx/findList')
    return $http.post(url, params);
  },
  // 兼容publicpage模式
  getHistoryUodateList(params) {
    let token = params.token
    if (token) {
      delete params.token
      return $http.post(getUrl('itShare', `/wfRptPlantask/approve/history/list`), params, null, {
        headers: { authorization: token }
      })
    } else {
      return $http.post(getUrl('itShare', `/wfRptPlantask/approve/history/list`), params)
    }
  },
  // 使用bpm token换取中台token
  getOauthTokenByBpmToken(params) {
    let url = getUrl('oauthService', '/oauth/api/token')
    return $http.post(url, params, null, {
      headers: { bpmp3token: params.bpmp3token }
    })
  },
  // 任务表上传后再次确认
  confirmUpload(params) {
    let url = getUrl('itShare', '/wfRptPlantask/excelImport/two')
    return $http.post(url, params);
  },

  // 获取大类
  getGroupType: () =>{
    return $http.get(getUrl('itShare', '/wfGroupRptPlantask/stype'))
  },

  // 获取小类
  getGroupMtype: (params) =>{
    return $http.get(getUrl('itShare', `/wfGroupRptPlantask/mtype/${params.code}`))
  },

  // 新增重点任务
  insertHighTask: (params) =>{
    return $http.post(getUrl('itShare', '/wfGroupRptPlantask/init/insert'),params)
  },

  // 编辑重点任务
  editHighTask: (params) =>{
    return $http.post(getUrl('itShare', '/wfGroupRptPlantask/admin/update'),params)
  },

  //获取重点任务详情
  getHighTaskById: (params) =>{
    return $http.get(getUrl('itShare', '/wfGroupRptPlantask/getById'),params)
  },

  // 保存重点任务
  savetHighTask: (params) =>{
    return $http.post(getUrl('itShare', '/wfGroupRptPlantask/init/save'),params)
  },

  // 发起任务
  initiateTask: (params) =>{
    return $http.post(getUrl('itShare', '/wfGroupRptPlantask/initiate'),params)
  },

  // 初始化校验任务
  initCheckTask: (params) =>{
    return $http.post(getUrl('itShare', '/wfGroupRptPlantask/init'),params)
  },

  // 汇总审批提交
  summaryAppSubmit: (params) =>{
    return $http.post(getUrl('itShare', '/wfGroupRptPlantask/gather/submit'),params)
  },

  // 批量提交
  batchSubmit: (params) =>{
    return $http.post(getUrl('itShare', '/wfGroupRptPlantask/batch/submit'),params)
  },

  //获取审批权限列表
  approverList: (params) =>{
    return $http.get(getUrl('itShare', '/wfGroupRptPlantask/approver'),params)
  },

  //获取填写权限列表
  submitterList: (params) =>{
    return $http.get(getUrl('itShare', '/wfGroupRptPlantask/submitter'),params)
  },

  //获取流程记录
  logList: (params) =>{
    return $http.get(getUrl('itShare', `/wfGroupRptPlantask/log/${params.id}`))
  },

  //导出
  exportTask: (params) =>{
    return $http.post(getUrl('itShare', '/wfGroupRptPlantask/export'),params)
  },

  //删除
  deleteTask: (params) =>{
    return $http.post(getUrl('itShare', '/wfGroupRptPlantask/delete'),params)
  },

  // 更新任务
  taskUpdate: (params) =>{
    return $http.post(getUrl('itShare', '/wfGroupRptPlantask/update'),params)
  },

  //提交审批
  taskSubmit: (params) =>{
    return $http.post(getUrl('itShare', '/wfGroupRptPlantask/submit'),params)
  },
};

export default coverApi;
